import AudioManager from "../Framework/Managers/AudioManager";
import { BundleManager, Bundles } from "../Framework/Managers/BundleManager";
import { DataManager } from "../Framework/Managers/DataManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import { ResourceUtil } from "../Framework/Utils/ResourceUtil";
import { InventoryType } from "./InventoryPanel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class InventoryButton extends cc.Component {
    Selected: cc.Node = null;
    DefaultIcon: cc.Node = null;
    Icon: cc.Sprite = null;
    Name_Lb: cc.Label = null;
    Desc_Lb: cc.Label = null;
    Bullet_Lb: cc.Label = null;

    type: InventoryType = InventoryType.None;

    protected onLoad(): void {
        this.Selected = NodeUtil.GetNode("Selected", this.node);
        this.DefaultIcon = NodeUtil.GetNode("DefaultIcon", this.node);
        this.Icon = NodeUtil.GetComponent("Icon", this.node, cc.Sprite);
        this.Name_Lb = NodeUtil.GetComponent("Name_Lb", this.node, cc.Label);
        this.Desc_Lb = NodeUtil.GetComponent("Desc_Lb", this.node, cc.Label);
        this.Bullet_Lb = NodeUtil.GetComponent("Bullet_Lb", this.node, cc.Label);
    }

    Set(type: InventoryType) {
        this.type = type;
    }

    Reset() {
        this.Desc_Lb.string = ``;
        this.Bullet_Lb && (this.Bullet_Lb.string = ``);
    }

    Refresh() {
        this.Reset();

        let data = null;
        let scale = 1;
        this.Icon.node.color = cc.color(255, 255, 255, 255);
        switch (this.type) {
            case InventoryType.Helmet:
                data = DataManager.PlayerData.Helmet;
                if (data) {
                    this.Icon.node.color = cc.color(255, 255 * (data.Durability / 100), 255 * (data.Durability / 100), 255);
                }
                scale = 0.6;
                break;
            case InventoryType.Bulletproof:
                data = DataManager.PlayerData.Bulletproof;
                if (data) {
                    this.Icon.node.color = cc.color(255, 255 * (data.Durability / 100), 255 * (data.Durability / 100), 255);
                }
                scale = 1.5;
                break;
            case InventoryType.Weapon_0:
                scale = 2;
                data = DataManager.PlayerData.Weapon_0;
                if (data) {
                    this.Bullet_Lb.string = `${data.Desc}`;
                    this.Desc_Lb.string = `${data.MagazineBulletCount}/${data.BulletCount}`;
                }
                break;
            case InventoryType.Weapon_1:
                scale = 2;
                data = DataManager.PlayerData.Weapon_1;
                if (data) {
                    this.Bullet_Lb.string = `${data.Desc}`;
                    this.Desc_Lb.string = `${data.MagazineBulletCount}/${data.BulletCount}`;
                }
                break;
            case InventoryType.Pistol:
                data = DataManager.PlayerData.Pistol;
                if (data) {
                    this.Bullet_Lb.string = `${data.Desc}`;
                    this.Desc_Lb.string = `${data.MagazineBulletCount}/${data.BulletCount}`;
                }
                break;
            case InventoryType.MeleeWeapon:
                data = DataManager.PlayerData.MeleeWeapon;
                break;
        }

        this.Name_Lb.string = data ? `${data.Name}` : `${this.type}`;
        this.DefaultIcon.active = data == null;
        this.Icon.node.active = data != null;

        if (data != null) {
            this.Icon.node.setScale(scale);
            BundleManager.LoadSpriteFrame(Bundles.Item, `${data.Type}_${data.Name}`).then((sf: cc.SpriteFrame) => {
                this.Icon.spriteFrame = sf;
            });
        }
    }

    RefreshSelected(type: InventoryType) {
        this.Selected.active = this.type == type;
    }
}
